var Kuo_pointsmall = {
    "name":"商城模块",
    "version":"1.0.0",
    product_Add:async ($CANSHU)=>{
        let $hash = 'safetoken/'+$CANSHU['Safe_AdminId'];
        let Safe_AdminId = await Mem.Get($hash);
        if(!Safe_AdminId  ||  Safe_AdminId  != $CANSHU['Safe_Token'] ){
            return false;
        }
        if(!isset($CANSHU['id'])){
            return false;
        }
        let $fan  =null;
        let $db =  await db("pointsmall_product");
        $fan = await  $db.Insert($CANSHU);
        return $fan;
    },shopsku_Get:async ($ID)=>{
        //获取shopsku
        let $db =  await db("pointsmall_shopsku");
        let sedata = await $db.Where({shopid:$ID}).Select();
        if(sedata){
            return sedata;
        }else{
            return [];
        }
        
    }, shopsku_Up:async ($CANSHU,$AC)=>{
        //add  put
        let baocun = 0;
        let ID = $CANSHU['id'];
        let ZUHE = {};
        for(let kk in $CANSHU){
            let kkvv = $CANSHU[kk];
            if(kk.indexOf("shop_") > -1){
                baocun++;
                let keys= kk.split("_");
                if(!ZUHE[keys['1']]){
                    ZUHE[keys['1']] = {
                        'sku':keys['1'],
                        'shopid':ID,
                        'danjia':0, //销售价格
                        'shiija':0, //市场价格
                        'benjia':0, //成本价格
                        'kucun':0, //库存
                        'integral':0,//赠送积分
                        'name':'',//编号
                        'photo':'',//图片
                        'remarks':'' //备注
                    };
                }

                if( keys['2'] == 'photo'){
                    kkvv =  kkvv.replace(CDNHOST,'/');
                }else  if( keys['2'] == 'name'){

                }else  if( keys['2'] == 'remarks'){

                }else{
                    kkvv = kkvv*1;
                }


                ZUHE[keys['1']] [keys['2']] =  kkvv;


                
            }else  if(kk.indexOf("SKU_") > -1){
                baocun++;
            }
        }
        if(baocun > 0){

            let $db =  await db("pointsmall_shopsku");
            let sedata = await $db.ZhiCha('sku,id').Where({shopid:ID}).Select();
            if(sedata){
                let sedata_ ={};
                for(let $k in sedata){
                    let $v = sedata[$k];
                    sedata_[$v['sku']] = $v['id'];
                }
                for(let $k in ZUHE){
                    let $v = ZUHE[$k];
                    if(sedata_[$v['sku']]){
                        await $db.Where({'id':sedata_[$v['sku']]}).Update($v);
                    }else{
                        await $db.Insert($v);
                    }
                }

            }else{

               for(let $k in ZUHE){
                   let $v = ZUHE[$k];
                   await $db.Insert($v);
               }
            }

        }else if($AC =='put'){
            let $db =  await db("pointsmall_shopsku");
            $db.ZhiCha('sku,id').Where({shopid:ID}).Delete();
        }

    },
    product_Put:async ($CANSHU)=>{
       
        let $hash = 'safetoken/'+$CANSHU['Safe_AdminId'];
        let Safe_AdminId = await Mem.Get($hash);
        if(!Safe_AdminId  ||  Safe_AdminId  != $CANSHU['Safe_Token'] ){
            return false;
        }
        if(!isset($CANSHU['id'])){
            return false;
        }

      

        let $fan  =null;
        let $db =  await db("pointsmall_product");
        let $shuju = await $db.Where({'id':$CANSHU['id']}).Find();
       
        if($shuju){
           $fan = await $db.Where({'id':$CANSHU['id']}).Update($CANSHU);
        }else{
            $fan = await  $db.Insert($CANSHU);
        }
        return $fan;

    },
    product_Del:async ($CANSHU)=>{
    
      
        let $hash = 'safetoken/'+$CANSHU['Safe_AdminId'];
        
        let Safe_AdminId = await Mem.Get($hash);
        
        if(!Safe_AdminId  ||  Safe_AdminId  != $CANSHU['Safe_Token'] ){
            
            return false;
        }
        if(!isset($CANSHU['id'])){
            return false;
        }
        let $db =  await db("pointsmall_product");
        return $db.Where({'id':$CANSHU['id']}).Delete();
        

    },  product_Get:async ($CANSHU)=>{
    
        
        let $hash = 'safetoken/'+$CANSHU['Safe_AdminId'];

   
      
        let Safe_AdminId = await Mem.Get($hash);
       
        if(!Safe_AdminId  ||  Safe_AdminId  != $CANSHU['Safe_Token'] ){
           
            return false;
        }
        if(!isset($CANSHU['id'])){
            return false;
        }
        let $db =  await db("pointsmall_product");

        let $jiegou = Kuo_MysqlTABLE.TaBle[MYSQLconfig.prefix+"pointsmall_product"];
        let $default ={};
        if($jiegou){
            for(let  $k in $jiegou ){
                let  $v  = $jiegou[$k];
                if($v == 'auto_increment'){
                   $v = 0;
                }
                $default[$k] =$v;
            }
        }

        for(let $k_ in $CANSHU){
            let $v_ =$CANSHU[$k_];
            if(isset($default[$k_])){
                $default[$k_] = $v_;
            }
        }

        let $shuju = {};
        if( $CANSHU['id'] > 0 ){
            $shuju = await $db.Where({'id':$CANSHU['id']}).Find();
            if(!$shuju){
                $shuju = $default;
                await $db.Insert($default);
            }
        }else{
            $shuju = $default;
        }

        let $features = await Kuoplus( "pointsmall" );
     
        let $DATA = [];
        $DATA.push(['pointsmall_integral','所需积分','text','','所需积分',$shuju['integral']]);
        $DATA.push(['pointsmall_restrict','限购天数','text','','限购天数',$shuju['restrict']]);
        $DATA.push(['pointsmall_num','限购数量','text','','限购数量',$shuju['num']]);
        $DATA.push(['pointsmall_inventory','商品库存 ','text','','商品库存 ',$shuju['inventory']]);
        $DATA.push(['pointsmall_sales','商品销量','text','','商品销量',$shuju['sales']]);
        $DATA.push(['pointsmall_type','处理方式','select','',($features['configure']['处理方式']?$features['configure']['处理方式']:[]),$shuju['type']]);
        $DATA.push(['pointsmall_parameter','处理参数','text','','处理参数',$shuju['parameter']]);
        $DATA.push(['pointsmall_remarks','商品备注','textarea','','商品备注',$shuju['remarks']]);
        
        return $DATA;
    }
    
};
module.exports = Kuo_pointsmall;